#pragma once

#include "map"
#include "comutil.h"
#include "DispatchImpl.h"

class CFunctionDispatch : public CDispatchImpl
{
public:
	typedef _variant_t (*TypeFunDispatch) (IWebBrowser2* pBrowser, IDispatch* pDisp, void* pParam);

	CFunctionDispatch(IWebBrowser2* pBrowser);

	void SetParam(void* pParam);
	void RegFun(LPCTSTR lpszFunName, TypeFunDispatch fun);
	
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
		LCID lcid, DISPID* rgdispid);
	
	STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
		LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
		EXCEPINFO* pexcepinfo, UINT* puArgErr);;
	
private:
	typedef std::map<_bstr_t, TypeFunDispatch> TypeFunDispatchMap;
	TypeFunDispatchMap m_lsFuns;
	IWebBrowser2* m_pBrowser;

	void* m_pParam;
};

class CMethodDispatch : public CDispatchImpl
{
public:
	typedef _variant_t (*TypeFunDispatch) (IWebBrowser2* pBrowser, IDispatch* pDisp, void* pParam);
	
	CMethodDispatch(IWebBrowser2* pBrowser, IDispatch* pFunctionDispatch, TypeFunDispatch pMethod, void* pParam);
	
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
		LCID lcid, DISPID* rgdispid);
	
	STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
		LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
		EXCEPINFO* pexcepinfo, UINT* puArgErr);;
	
private:
	TypeFunDispatch m_fun;
	CComPtr<IDispatch> m_spFunctionDispatch;
	IWebBrowser2* m_pBrowser;

	void* m_pParam;
};